/*
**  This file is part of FatRemote.
**
**  FatRemote is free software: you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation, either version 3 of the License, or
**  (at your option) any later version.
**
**  FatRemote is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with FatRemote.  If not, see <http://www.gnu.org/licenses/>. 
*/
    
package com.mk.FatRemote;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Remote extends Activity
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		m_hostTextbox = (EditText)findViewById(R.id.txtHost);

		m_backButton = (Button)findViewById(R.id.btnBack); 
		m_downButton = (Button)findViewById(R.id.btnDown); 
		m_ffwdButton = (Button)findViewById(R.id.btnFfwd); 
		m_homeButton = (Button)findViewById(R.id.btnHome); 
		m_infoButton = (Button)findViewById(R.id.btnInfo); 
		m_leftButton = (Button)findViewById(R.id.btnLeft); 
		m_menuButton = (Button)findViewById(R.id.btnMenu); 
		m_muteButton = (Button)findViewById(R.id.btnMute); 
		m_nextTrackButton = (Button)findViewById(R.id.btnNextTrack); 
		m_okButton = (Button)findViewById(R.id.btnOK); 
		m_playButton = (Button)findViewById(R.id.btnPlay); 
		m_powerButton = (Button)findViewById(R.id.btnPower); 
		m_prevTrackButton = (Button)findViewById(R.id.btnPrevTrack); 
		m_rewindButton = (Button)findViewById(R.id.btnRewind); 
		m_rightButton = (Button)findViewById(R.id.btnRight); 
		m_stopButton = (Button)findViewById(R.id.btnStop); 
		m_upButton = (Button)findViewById(R.id.btnUp); 
		m_volDownButton = (Button)findViewById(R.id.btnVolDown); 
		m_volUpButton = (Button)findViewById(R.id.btnVolUp); 
		m_zoomInButton = (Button)findViewById(R.id.btnZoomIn); 
		m_zoomOutButton = (Button)findViewById(R.id.btnZoomOut);
		
		
		m_backButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(BACK_KEY); } });
		m_downButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(DOWN_KEY); } });
		m_ffwdButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(FFWD_KEY); } });
		m_homeButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(HOME_KEY); } });
		m_infoButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(INFO_KEY); } });
		m_leftButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(LEFT_KEY); } });
		m_menuButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(MENU_KEY); } });
		m_muteButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(MUTE_KEY); } });
		m_nextTrackButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(TRACKNEXT_KEY); } });
		m_okButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(OK_KEY); } });
		m_playButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(PLAY_KEY); } });
		m_powerButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(POWER_KEY); } });
		m_prevTrackButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(TRACKPREV_KEY); } });
		m_rewindButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(REW_KEY); } });
		m_rightButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(RIGHT_KEY); } });
		m_stopButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(STOP_KEY); } });
		m_upButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(UP_KEY); } });
		m_volDownButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(VOLDOWN_KEY); } });
		m_volUpButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(VOLUP_KEY); } });
		m_zoomInButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(ZOOMIN_KEY); } });
		m_zoomOutButton.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { SendKey(ZOOMOUT_KEY); } });
	}



	//  TCP keycodes courtesy of hexcide @ forums.seagate.com
	//  http://forums.seagate.com/t5/FreeAgent-Theater/iphone-remote-app/td-p/87810/highlight/false/page/2
	//
	//	Commands are 4 bytes sent to TCP port 9999 of the format
	//	48 12 XX 00
	//	where XX varies for each command thus:
	//	 
	//	07: left
	//	06: right
	//	40: up
	//	41: down
	//	1B: back
	//	30: end
	//	51: ff
	//	39: home
	//	47: info
	//	1A: menu
	//	59: mute
	//	0E: ok
	//	54: pause
	//	4F: play
	//	38: power
	//	52: rew
	//	31: start
	//	33: voldown
	//	32: volup
	//	35: zoomin
	//	08: zoomout
	//
	private void SendKey(byte key)
	{
		byte b[] = {B0,B1,key,B3};
		
		Socket s = null;
		OutputStream o = null;
		
		try
		{
			SocketAddress address = new InetSocketAddress(m_hostTextbox.getText().toString(), 9999);
			s = new Socket();
			s.connect(address, 1000);
			o = s.getOutputStream();
			o.write(b);
		}
		catch (UnknownHostException x)
		{
			AlertBox("Where's your Player?", x.getMessage());
		}
		catch (IOException x)
		{
			AlertBox("Is the Player turned off?", x.toString());
		}
		finally
		{
			try
			{
				if (null != o)
				{
					o.flush();
					o.close();
				}
				
				if (null != s)
				{
					s.close();
				}
			}
			catch (Exception x)
			{
				//AlertBox("Error Closing Sockets", x.toString());
			}
		}
	}
	

    protected void AlertBox(String title, String mymessage) 
    {
    	AlertDialog.Builder d = new AlertDialog.Builder(this);
		d.setMessage(mymessage);
		d.setTitle(title);
		d.setNeutralButton(	android.R.string.ok,
							new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton){}}
						  );
		d.show(); 
    }	
	
    
	private EditText m_hostTextbox;
	
	private Button m_backButton; 
	private Button m_downButton; 
	private Button m_nextTrackButton; 
	private Button m_ffwdButton; 
	private Button m_homeButton; 
	private Button m_infoButton; 
	private Button m_leftButton; 
	private Button m_menuButton; 
	private Button m_muteButton; 
	private Button m_okButton; 
	private Button m_playButton; 
	private Button m_powerButton; 
	private Button m_rewindButton; 
	private Button m_rightButton; 
	private Button m_prevTrackButton; 
	private Button m_stopButton; 
	private Button m_upButton; 
	private Button m_volDownButton; 
	private Button m_volUpButton; 
	private Button m_zoomInButton; 
	private Button m_zoomOutButton;

	private static final byte POWER_KEY = (byte)0x38; 
	private static final byte HOME_KEY = (byte)0x39; 
	private static final byte MENU_KEY = (byte)0x1A; 
	private static final byte INFO_KEY = (byte)0x47; 
	private static final byte BACK_KEY = (byte)0x1B; 
	private static final byte LEFT_KEY = (byte)0x07; 
	private static final byte RIGHT_KEY = (byte)0x06; 
	private static final byte UP_KEY = (byte)0x40; 
	private static final byte DOWN_KEY = (byte)0x41; 
	private static final byte OK_KEY = (byte)0x0E; 
	private static final byte STOP_KEY = (byte)0x54; 
	private static final byte PLAY_KEY = (byte)0x4F; 
	private static final byte FFWD_KEY = (byte)0x51; 
	private static final byte REW_KEY = (byte)0x52; 
	//private static final byte SKIPFWD30_KEY = ; 
	//private static final byte SKIPBCK10_KEY = ; 
	private static final byte TRACKPREV_KEY = (byte)0x31; 
	private static final byte TRACKNEXT_KEY = (byte)0x30; 
	private static final byte VOLDOWN_KEY = (byte)0x33; 
	private static final byte VOLUP_KEY = (byte)0x32; 
	private static final byte MUTE_KEY = (byte)0x59; 
	private static final byte ZOOMIN_KEY = (byte)0x35; 
	private static final byte ZOOMOUT_KEY = (byte)0x08; 

	// Default byte values
	private static final byte B0 = (byte)0x48;
	private static final byte B1 = (byte)0x12;
	private static final byte B3 = (byte)0x00;
}
